﻿using System;
using System.Collections.Generic;
using AnjLab.SyncIT.Core.Api;
using AnjLab.SyncIT.Core.Data.GoogleCode;
using NUnit.Framework;

namespace AnjLab.SyncIT.Core.Tests.Data.GoogleCode
{
    [TestFixture]
    public class TestGoogleCodeIssueParser
    {
        [Test]
        public void TestParseFirst()
        {
            var source = new Source {ProjectBase = "dummy"};
            var parser = new GoogleCodeIssueParser(source);

            IList<Issue> issues = parser.ParseIssueList(
                "file:///"
                + AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/')
                + "/data/googlecode/list.htm");

            Assert.AreEqual(4, issues.Count);

            Assert.AreEqual("2", issues[0].ID);
            Assert.AreEqual("1", issues[1].ID);
            Assert.AreEqual("4", issues[2].ID);
            Assert.AreEqual("3", issues[3].ID);
        }
    
        [Test]
        public void TestParseFirstEmptyList()
        {
            var source = new Source { ProjectBase = "dummy" };

            var parser = new GoogleCodeIssueParser(source);

            IList<Issue> issues = parser.ParseIssueList(
                "file:///"
                + AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/')
                + "/data/googlecode/empty-list.htm");

            Assert.AreEqual(0, parser.CurrentRange.From);
            Assert.AreEqual(0, parser.CurrentRange.To);
            Assert.AreEqual(0, parser.TotalCount);

            Assert.AreEqual(0, issues.Count);
        }

        [Test]
        public void TestParseIssueDetails()
        {
            var source = new Source { ProjectBase = "dummy" };

            var parser = new GoogleCodeIssueParser(source);

            Issue issue = parser.ParseIssueDetails("file:///"
                                                   + AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/')
                                                   + "/data/googlecode/detail.htm");

            Assert.AreEqual("1", issue.ID);
            Assert.AreEqual(1, issue.Stars);
            Assert.AreEqual("----", issue.Owner);
            Assert.AreEqual("New", issue.Status);
            Assert.AreEqual("dmitry.gusev, dmitry.gusev", issue.CC);
            Assert.AreEqual("Сделать выбор категорий задач из списка", issue.Summary);
            Assert.IsNotNull(issue.Labels);
            Assert.AreEqual(3, issue.Labels.Length);
            Assert.AreEqual("Type-Defect", issue.Labels[0]);
            Assert.AreEqual("Priority-Medium", issue.Labels[1]);
            Assert.AreEqual("Component-Logic", issue.Labels[2]);
            Assert.AreEqual("dmitry.gusev", issue.Reporter);
            Assert.AreEqual(
                @"У аутлука есть окошко &quot;Цветовые категории&quot; (вложение 1)
И еще есть контрол, который позволяет отображать список категорий (вложение 2)

Оба контрола позволяют работать с несколькими категориями сразу.
",
                issue.Description);
            Assert.AreEqual("Wed Mar 12 14:21:09 2008", issue.Opened);
            Assert.AreEqual("Sun Mar 16 12:32:26 2008", issue.Modified);
            Assert.AreEqual("----", issue.Closed);
        }

        [Test]
        public void TestParseIssueDetailsWithMoreThanOneStars()
        {
            var source = new Source { ProjectBase = "dummy" };

            var parser = new GoogleCodeIssueParser(source);

            Issue issue = parser.ParseIssueDetails("file:///"
                                                   + AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/')
                                                   + "/data/googlecode/detail15.htm");

            Assert.AreEqual("15", issue.ID);
            Assert.AreEqual(2, issue.Stars);
        }

        [Test]
        public void TestParseAttachmentsInComments()
        {
            var source = new Source { ProjectBase = "outlooktrac" };

            var parser = new GoogleCodeIssueParser(source);

            var issue = parser.ParseIssueDetails("file:///"
                                                   + AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/')
                                                   + "/data/googlecode/detail7.htm");

            Assert.AreEqual(1, issue.Attachments.Length);
            Assert.AreEqual("fill-outlook-taskitem-details-from-issue.jpg", issue.Attachments[0].Name);
            Assert.AreEqual("93.6 KB ", issue.Attachments[0].Size);
            Assert.AreEqual("http://code.google.com/p/outlooktrac/issues/attachment?aid=5214180014756742196&name=fill-outlook-taskitem-details-from-issue.jpg", issue.Attachments[0].URL);
        }
    }
}